//
//  ViewController.swift
//  PhotoBrowser
//
//  Created by meng on 16/5/17.
//  Copyright © 2016年 leotao. All rights reserved.
//

import UIKit
import Kingfisher
import AssetsLibrary

class ViewController: UIViewController {
    var dataSource = {
        return ["photo1.jpg", "photo2.jpg", "photo3.jpg", "photo4.jpg", "photo5.jpg", "photo6.jpg", "photo7.jpg", "photo8.jpg", "short1", "short2", "short3"];
    }();
    
    @IBOutlet weak var tableView: UITableView!
    var progressView: CircleProgressView!;
    
    @IBOutlet weak var testImageView: UIImageView!
    
    var dataSource2 = [Photo]();

    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        initialArray();
        
//        self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor();
//        self.navigationController?.navigationBar.hidden = true;
//        self.navigationController?.navigationBar.translucent = true;
//        self.navigationController?.navigationBar.barStyle = .BlackTranslucent;
    }
    
    func delay(delay:Double, closure:()->()) {
        dispatch_after(
            dispatch_time(
                DISPATCH_TIME_NOW,
                Int64(delay * Double(NSEC_PER_SEC))
            ),
            dispatch_get_main_queue(), closure)
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated);
        
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func pushNoti(sender: AnyObject) {
        NSNotificationCenter.defaultCenter().postNotificationName("Test", object: nil);
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
           }
    
    func initialArray() {
        let imageUrl = ["http://7xr16i.com1.z0.glb.clouddn.com/photo1.jpg",
                        "http://7xr16i.com1.z0.glb.clouddn.com/photo2.jpg",
                        "http://7xr16i.com1.z0.glb.clouddn.com/photo3.jpg",
                        "http://7xr16i.com1.z0.glb.clouddn.com/photo4.jpg",
                        "http://7xr16i.com1.z0.glb.clouddn.com/photo5.jpg",
                        "http://7xr16i.com1.z0.glb.clouddn.com/photo6.jpg",
                        "http://7xr16i.com1.z0.glb.clouddn.com/photo7.jpg",
                        "http://7xr16i.com1.z0.glb.clouddn.com/photo8.jpg",
                        "http://7xr16i.com1.z0.glb.clouddn.com/short1.png",
                        "http://7xr16i.com1.z0.glb.clouddn.com/short2.png",
                        "http://7xr16i.com1.z0.glb.clouddn.com/short3.png"];
        
        for url in imageUrl {
            let photo = Photo(url: NSURL(string: url)!);
            dataSource2.append(photo);
        }
    }
}

extension ViewController: PhotoBrowserDelegate {
    func numberOfPhotos() -> Int {
        return dataSource2.count;
    }
    
    func photoAtIndex(index: Int) -> Photo {
        
        let photo = self.dataSource2[index];
        
        return photo;
    }
}

extension ViewController: UITableViewDataSource, UITableViewDelegate {
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2;
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let identifier = "cell";
        var cell = tableView.dequeueReusableCellWithIdentifier(identifier);
        if cell == nil {
            cell = UITableViewCell(style: .Default, reuseIdentifier: identifier);

        }
        
        if indexPath.row == 0 {
            cell?.textLabel?.text = "网络图片";
        } else if indexPath.row == 1 {
            cell?.textLabel?.text = "系统相册";
        }
        
        return cell!;
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true);
        if indexPath.row == 0 {
            let browser = PhotoBrowser(delegate:self);
            
            self.navigationController?.pushViewController(browser, animated: true);
        } else if indexPath.row == 1 {
            let albumList = AlbumListVC();
            self.navigationController?.pushViewController(albumList, animated: true);
        }
    }
}
